package cn.wolfcode.wolf2w.business.controller;

import cn.wolfcode.wolf2w.business.api.RemoteStrategyEsService;
import cn.wolfcode.wolf2w.business.api.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.business.api.domain.StrategyContent;
import cn.wolfcode.wolf2w.business.api.domain.StrategyEs;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.business.service.IStrategyContentService;
import cn.wolfcode.wolf2w.common.core.constant.SecurityConstants;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.security.annotation.InnerAuth;
import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.query.StrategyQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyService;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 攻略 Controller
 * 
 * @author LSY
 * @date 2025-09-16
 */
@RestController
@RequestMapping("strategies")
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyContentService strategyContentService;
    @Autowired
    private RemoteStrategyEsService remoteStrategyEsService;


    @GetMapping("/dataInit")
    public R<?> dataInit(){
        List<Strategy> list = strategyService.list();
        for (Strategy strategy : list) {
            StrategyEs strategyEs = new StrategyEs();
            BeanUtils.copyProperties(strategy, strategyEs);
            remoteStrategyEsService.initStrategyEs(strategyEs, SecurityConstants.INNER);
        }
        return R.ok("攻略数据初始化完成");
    }







    //thumbsup
    @PostMapping("/thumbsup")
    @RequiresLogin
    public R<Map<String, Object>> thumbsup(Long sid){
        Map<String, Object> map=strategyService.thumbsup(sid);
        return R.ok(map);
    }

    //isUserFavor
    @GetMapping("/isUserFavor")
    @RequiresLogin
    public R<Boolean> isUserFavor(Long sid, Long uid){
        Boolean b=strategyService.isUserFavor(sid,uid);
        return R.ok(b);
    }

    //favor
    @PostMapping("/favor")
    @RequiresLogin
    public R<Map<String, Object>> favor(Long sid){
        Map<String, Object> map=strategyService.favor(sid);
        return R.ok(map);
    }
    //replynumIncr
    @PostMapping("/replynumIncr")
    public R<Map<String, Object>> replynumIncr(Long sid){
        Map<String, Object> map=strategyService.replynumIncrease(sid);
        return R.ok(map);
    }

    @PostMapping("/viewnumIncr")
    public R<Map<String, Object>> viewnumIncr(Long sid){
        Map<String, Object> map=strategyService.viewnumIncrease(sid);
        return R.ok(map);
    }


    @GetMapping("/viewnnumTop3")
    public R<List<Strategy>> viewnnumTop3(Long destId){
        List<Strategy> list=strategyService.queryViewnnumTop3(destId);
        return R.ok(list);
    }

    @GetMapping("/catalogList")
    public R<List<StrategyCatalog>> catalogList(Long destId){
        List<StrategyCatalog> list = strategyCatalogService.queryCatalogList(destId);
        return R.ok(list);
    }
    /**
     * 攻略详情
     */
    @GetMapping("/detail/{id}")
    public R<Strategy> detail(@PathVariable Long id) {
        Strategy strategy = strategyService.getById(id);
        StrategyContent content = strategyContentService.getById(id);
        strategy.setContent(content);
        return R.ok(strategy);
    }
    /**
     * 攻略 列表(defaultValue = "0")
     */
    @GetMapping("/query")
    public R<IPage<Strategy>> query(StrategyQuery qo) {
        IPage<Strategy> page = strategyService.queryPage(qo);
        return R.ok(page);
    }


    /*****************************************对外暴露Fegin接口**********************************************/
    /**
     * Feign 接口
     */
    @GetMapping("/feign/list")
    public R<List<Strategy>> feignList() {
        return R.ok(strategyService.list());
    }
    @InnerAuth
    @GetMapping("/feign/{id}")
    public R<Strategy> feignGet(@PathVariable Long id) {
        return R.ok(strategyService.getById(id));
    }
    @PostMapping("/feign/statisRank")
    public R<?> statisRank(){
        System.out.println("strategyController中statisRank方法被调用");
        strategyService.statisRank();
        return R.ok();
    }

    //条件导航
    @PostMapping("/feign/statisCondition")
    public R<?> statisCondition(){
        System.out.println("strategyController中statisCondition方法被调用");
        strategyService.statisCondition();
        return R.ok();
    }
    //攻略统计数据持久化
    @PostMapping("/feign/strategyStatisHashPersistence")
    public R<?> strategyStatisHashPersistence(){
//        System.out.println("strategyController中strategyStatisHashPersistence方法被调用");
        strategyService.strategyStatisHashPersistence();
        return R.ok();
    }


}
